package br.com.unitunes.model;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity(name = "ALBUM")
@SequenceGenerator(sequenceName = "ALBUM_SEQ", name = "ALBUM_SEQ", allocationSize=1, initialValue = 1)
public class Album extends GenericModel {

	@Id
	@Column(name = "ALBUM_ID")
	@GeneratedValue(generator = "ALBUM_SEQ", strategy = GenerationType.SEQUENCE)	
	private Long id;	
	
	@Column(name = "NOME", length = 100, nullable = true)	
	private String nome;
	
	@Column(name = "DESCRICAO", length = 400, nullable = true)	
	private String descricao;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DATA_CREATE", nullable = true)
    private Date dataCreate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DATA_UPDATE", nullable = true)
    private Date dataUpdate;

	@OneToMany(mappedBy = "album", targetEntity = Musica.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)  
    private List<Musica> musicas; 
	
    @ManyToOne
    @JoinColumn(name = "AUTOR_ID")
    private Usuario autor;	    
    
    @PrePersist
    protected void onCreate() {
    	dataCreate = dataUpdate = new Date();
    }

    @PreUpdate
    protected void onUpdate() {
    	dataUpdate = new Date();
    }
	
	@Override
	public Long getId() {
		return this.id;
	}

	@Override
	public void setId(Long id) {
		this.id = id;
	}

	public List<Musica> getMusicas() {
		return musicas;
	}

	public void setMusicas(List<Musica> musicas) {
		this.musicas = musicas;
	}

	public Usuario getAutor() {
		return autor;
	}

	public void setAutor(Usuario autor) {
		this.autor = autor;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public Date getDataCreate() {
		return dataCreate;
	}

	public void setDataCreate(Date dataCreate) {
		this.dataCreate = dataCreate;
	}

	public Date getDataUpdate() {
		return dataUpdate;
	}

	public void setDataUpdate(Date dataUpdate) {
		this.dataUpdate = dataUpdate;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}
}
